package cn.itsource.org.controller;


import cn.itsource.basic.util.JsonResult;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工的控制层
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工管理")
public class EmployeeController {

    /**
     * 注入service对象
     */
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询所有员工经理
     */
    @GetMapping("/findManager")
    @ApiOperation(value = "查询所有员工经理",notes = "可以查询所有的员工经理")
    public JsonResult findManager() {
        return JsonResult.createSuccess(employeeService.findManager());
    }

    /**
     * 查询所有
     * @return 员工列表
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有员工",notes = "可以查询所有的员工")
    public JsonResult findAll(){
        return JsonResult.createSuccess(employeeService.findAll());
    }


    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询",notes = "可以分页查询所有的员工")
    public JsonResult findPage(@RequestBody EmployeeQuery query){
        return JsonResult.createSuccess(employeeService.findPage(query));
    }

    /**
     * 新增或者修改,根据id区分
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改",notes = "可以根据id区分操作")
    public JsonResult saveOrUpdate(@RequestBody Employee employee){

        //根据id区分,没有id就是新增
        try {
            employeeService.saveOrUpdate(employee);

            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }

    /**
     * 删除员工根据id
     * @return
     * 接受请求： /emp/12
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作",notes = "根据id删除员工")
    public JsonResult delete(@PathVariable("id") Long id){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            employeeService.delete(id);

            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @PostMapping("batchRemove")
    public JsonResult batchRemove(@RequestBody EmployeeQuery query){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            employeeService.batchRemove(query);

            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @GetMapping("findShopAllEmployee")
    public JsonResult findShopAllEmployee(){
        return JsonResult.createSuccess(employeeService.findShopAllEmployee());
    }

}